package com.cn.kehong.service;


import com.cn.kehong.api.ITerminalLicenseService;
import com.cn.kehong.dao.TerminalLicenseMapper;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TerminalLicenseServiceImpl implements ITerminalLicenseService {

    @Autowired
    private TerminalLicenseMapper terminalLicenseMapper;


    @Override
    public List<TerminalLicense> getTerminalLicenselList(QueryForm queryForm) throws Exception {
        int count = terminalLicenseMapper.getTerminalLicenselCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<TerminalLicense> terminalLicenses = terminalLicenseMapper.getTerminalLicenselList(queryForm);
        if (terminalLicenses.size()>0){
            terminalLicenses.get(0).setCount(count);
        }
        return terminalLicenses;
    }

    @Override
    public String addTerminalLicense(List<TerminalLicense> terminalLicense) throws Exception {
        List<String> macAddressList = Lists.newArrayList();
        for (TerminalLicense terminalLicense1:terminalLicense){
            String result = this.terminalLicenseAddChange(terminalLicense1);
            if (!"".equals(result)){
                return result;
            }

            String id = CommonUtil.generateRandomNum("terminalLicense_");
            terminalLicense1.setId(id);
            terminalLicense1.setStatus(Constants.NORMAL_STATUS);
            terminalLicense1.setCreateTime(new Date());
            terminalLicense1.setCreateBy(terminalLicense1.getCreateBy());
            macAddressList.add(terminalLicense1.getMacAddress());
        }
        TerminalLicenseExample example = new TerminalLicenseExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andMacAddressIn(macAddressList);
        int existsCount = terminalLicenseMapper.countByExample(example);
        if (existsCount > 0){
            return "Mac地址已存在！";
        }
        int count = terminalLicenseMapper.batchInsert(terminalLicense);

        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteTerminalLicense(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if (CollectionsUtil.isEmpty(ids)){
            return "请选中要删除的终端信息！";
        }
        TerminalLicense terminalLicense = new TerminalLicense();
        terminalLicense.setStatus(Constants.DEL_STATUS);
        TerminalLicenseExample example = new TerminalLicenseExample();
        example.createCriteria().andIdIn(ids);
        int count = terminalLicenseMapper.updateByExampleSelective(terminalLicense,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateTerminalLicense(TerminalLicense terminalLicense) throws Exception {
        String id = terminalLicense.getId();

        if (StringUtils.isEmpty(id)){
            return "id is null";
        }
        String result = this.terminalLicenseAddChange(terminalLicense);
        //判断修改的数据是否满足字段的必填条件
        if (StringUtils.isNotEmpty(result)){
            return result;
        }
        if (this.countMac(terminalLicense.getMacAddress(),id) > 0){
            return "Mac地址已存在";
        }
        TerminalLicenseExample example = new TerminalLicenseExample();
        example.createCriteria().andIdEqualTo(id).andStatusNotEqualTo(Constants.DEL_STATUS);

        int count = terminalLicenseMapper.updateByExampleSelective(terminalLicense,example);
        return CommonUtil.outStr(count);
    }


    private int countMac(String code,String id) {
        TerminalLicenseExample example = new TerminalLicenseExample();
        TerminalLicenseExample.Criteria criteria = example.createCriteria();

        criteria.andStatusNotEqualTo(Constants.DEL_STATUS).andMacAddressEqualTo(code);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }

        int count = terminalLicenseMapper.countByExample(example);
        return count;
    }
    @Override
    public Map<String, Object> getTerminalLicenseCount() throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        TerminalLicenseExample licenseExample = new TerminalLicenseExample();
        licenseExample.createCriteria().andStatusNotEqualTo(Constants.DEL_STATUS);
        int count = terminalLicenseMapper.countByExample(licenseExample);

        TerminalLicenseExample licenseExample1 = new TerminalLicenseExample();
        licenseExample1.createCriteria().andXkStatusEqualTo("0");
        int ty = terminalLicenseMapper.countByExample(licenseExample1);

        TerminalLicenseExample licenseExample2 = new TerminalLicenseExample();
        licenseExample2.createCriteria().andXkStatusEqualTo("1");
        int qy = terminalLicenseMapper.countByExample(licenseExample2);
        resultMap.put("zc",count);
        resultMap.put("ty",ty);
        resultMap.put("qy",qy);
        return resultMap;
    }


    private String terminalLicenseAddChange(TerminalLicense terminalLicense){

        String result = "";

        if (StringUtils.isBlank(terminalLicense.getDeptId())){
            result = "请选择使用部门";
            return result;
        }
        if (StringUtils.isBlank(terminalLicense.getTerminalAme())){
            result = "请输入终端名称";
            return result;
        }
        if (StringUtils.isBlank(terminalLicense.getMacAddress())){
            result = "请输入Mac地址";
            return result;
        }
        return result;
    }
}
